#include "nr_util.h"


#ifdef Q_OS_WIN

#include <windows.h>

extern "C" IMAGE_DOS_HEADER __ImageBase;

/*****************************************************************************
        Return the executive full path, only works in windows
*****************************************************************************/    
char *get_exe_path(void)
{    
    /* Get current  module's (exe or dll) full path */
    static char path[MAX_PATH]="";
    static int is_initialied = 0; 
    int path_size = 0; 
    if (is_initialied)
        return path;
    
    GetModuleFileNameA((HMODULE)(&__ImageBase), path, MAX_PATH);

    /* 
        got the path only, cut the file name 
        reverse find the "\"    
    */
    path_size = strlen(path);
    Q_ASSERT(path_size > 0);
    path_size--;

    while(path_size)
    {
        if (path[path_size] == '\\')
        {
            /* Set to the end of string */
            path[path_size] = '\0';
            break;
        }
        path_size--;
    }

    /* Should not over before finding a character */
    Q_ASSERT(path_size > 0);

    is_initialied = 1;
    return path;
}

#else
    #error("Not implement the install dir in this platform")
#endif

static QString g_install_dir;

nr_util::nr_util()
{
}

QString nr_util::get_install_dir()
{
    if (g_install_dir.isEmpty())
        g_install_dir = get_exe_path();

    return g_install_dir;
}